// 云函数入口文件
const cloud = require('wx-server-sdk')
const got = require('got')
cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  let url = 'https://m.youdao.com/dict?le=jap&q=' + event.word;
  let web_trans_url = 'https://m.youdao.com/singledict?q=' + event.word + '&dict=web_trans&le=jap&more=false';
  try {
    let options = {
      method: 'GET',
      headers: {
        'User-Agent': 'Mozilla/5.0 (Linux; Android 7.0; SM-G892A Build/NRD90M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/67.0.3396.87 Mobile Safari/537.36',
      }
    }
    let basic = await got(encodeURI(url), options);
    let web = await got(encodeURI(web_trans_url), options);
    return {
      basic: basic.body,
      web: web.body,
    }
  } catch (e) {
    return {
      _log: e,
    }
  }
}